<template>
  <el-dialog width="60%" :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-col :span="12">
          <!-- 主题ID -->
          <el-form-item label="主题ID" prop="themeId">
            <!--文本框-->
            <el-input v-model="dataForm.themeId" placeholder="主题ID" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f1 -->
          <el-form-item label="f1" prop="f1">
            <!--文本框-->
            <el-input v-model="dataForm.f1" placeholder="f1" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f2 -->
          <el-form-item label="f2" prop="f2">
            <!--文本框-->
            <el-input v-model="dataForm.f2" placeholder="f2" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f3 -->
          <el-form-item label="f3" prop="f3">
            <!--文本框-->
            <el-input v-model="dataForm.f3" placeholder="f3" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f4 -->
          <el-form-item label="f4" prop="f4">
            <!--文本框-->
            <el-input v-model="dataForm.f4" placeholder="f4" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f5 -->
          <el-form-item label="f5" prop="f5">
            <!--文本框-->
            <el-input v-model="dataForm.f5" placeholder="f5" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f6 -->
          <el-form-item label="f6" prop="f6">
            <!--文本框-->
            <el-input v-model="dataForm.f6" placeholder="f6" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f7 -->
          <el-form-item label="f7" prop="f7">
            <!--文本框-->
            <el-input v-model="dataForm.f7" placeholder="f7" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f8 -->
          <el-form-item label="f8" prop="f8">
            <!--文本框-->
            <el-input v-model="dataForm.f8" placeholder="f8" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f9 -->
          <el-form-item label="f9" prop="f9">
            <!--文本框-->
            <el-input v-model="dataForm.f9" placeholder="f9" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f10 -->
          <el-form-item label="f10" prop="f10">
            <!--文本框-->
            <el-input v-model="dataForm.f10" placeholder="f10" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f11 -->
          <el-form-item label="f11" prop="f11">
            <!--文本框-->
            <el-input v-model="dataForm.f11" placeholder="f11" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f12 -->
          <el-form-item label="f12" prop="f12">
            <!--文本框-->
            <el-input v-model="dataForm.f12" placeholder="f12" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f13 -->
          <el-form-item label="f13" prop="f13">
            <!--文本框-->
            <el-input v-model="dataForm.f13" placeholder="f13" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f14 -->
          <el-form-item label="f14" prop="f14">
            <!--文本框-->
            <el-input v-model="dataForm.f14" placeholder="f14" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f15 -->
          <el-form-item label="f15" prop="f15">
            <!--文本框-->
            <el-input v-model="dataForm.f15" placeholder="f15" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f16 -->
          <el-form-item label="f16" prop="f16">
            <!--文本框-->
            <el-input v-model="dataForm.f16" placeholder="f16" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f17 -->
          <el-form-item label="f17" prop="f17">
            <!--文本框-->
            <el-input v-model="dataForm.f17" placeholder="f17" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f18 -->
          <el-form-item label="f18" prop="f18">
            <!--文本框-->
            <el-input v-model="dataForm.f18" placeholder="f18" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f19 -->
          <el-form-item label="f19" prop="f19">
            <!--文本框-->
            <el-input v-model="dataForm.f19" placeholder="f19" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- f20 -->
          <el-form-item label="f20" prop="f20">
            <!--文本框-->
            <el-input v-model="dataForm.f20" placeholder="f20" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      dataForm: {
        id: '',
        themeId: '',
        f1: '',
        f2: '',
        f3: '',
        f4: '',
        f5: '',
        f6: '',
        f7: '',
        f8: '',
        f9: '',
        f10: '',
        f11: '',
        f12: '',
        f13: '',
        f14: '',
        f15: '',
        f16: '',
        f17: '',
        f18: '',
        f19: '',
        f20: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        themeId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/cpn/cpnData2/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/cpn/cpnData2/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
